package com.android.dialer.extendboard;

import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;

import com.android.dialer.common.LogUtil;

import java.util.List;

public class Utils {
    static int hookState = 0; // 0 hook_on, 1 hook_off

    static public int getKeyPadFromKeyCode(int keyCode) {
        int keyIndex = -1;
        final int[] keyCodes = new int[] {
                ServiceConstant.SM_P07_KEY_0,
                ServiceConstant.SM_P01_KEY_1,
                ServiceConstant.SM_P00_KEY_2,
                ServiceConstant.SM_P17_KEY_3,
                ServiceConstant.SM_P02_KEY_4,
                ServiceConstant.SM_P03_KEY_5,
                ServiceConstant.SM_P16_KEY_6,
                ServiceConstant.SM_P04_KEY_7,
                ServiceConstant.SM_P05_KEY_8,
                ServiceConstant.SM_P15_KEY_9,
                ServiceConstant.SM_P06_KEY_STAR,
                ServiceConstant.SM_P14_KEY_POUND,
        };

        for (int i = 0; i < keyCodes.length; i++) {
            if (keyCodes[i] == keyCode) {
                keyIndex = i;
                break;
            }
        }

        return keyIndex;
    }

    static public void resetAudio(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        AudioDeviceInfo audioDeviceInfo = null;
        List<AudioDeviceInfo> audioDevices = audioManager.getAvailableCommunicationDevices();

        audioManager.clearCommunicationDevice();

        for (AudioDeviceInfo device : audioDevices) {
            if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
                audioDeviceInfo = device;
                break;
            }
        }

        if (audioDeviceInfo != null) {
            audioManager.setMode(AudioManager.MODE_NORMAL);
            audioManager.setCommunicationDevice(audioDeviceInfo);
        }
    }


    static public boolean isAudioRouteSpeaker(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        AudioDeviceInfo currentDevice = audioManager.getCommunicationDevice();

        if (currentDevice != null) {
            if (currentDevice.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
                return true;
            }
        }

        return false;
    }

    static public void checkAudioRoute(Context context, int hookState) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        AudioDeviceInfo audioDeviceInfo = null;
        List<AudioDeviceInfo> audioDevices = audioManager.getAvailableCommunicationDevices();
        int audioType = 0;

        LogUtil.i("Utils", "hookState:" + hookState + " checkAudioRoute: audioDevices size = " + audioDevices.size());
        audioManager.clearCommunicationDevice();

        if (hookState == 1) {
            audioType = AudioDeviceInfo.TYPE_BUILTIN_SPEAKER;
        }else {
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            audioType = AudioDeviceInfo.TYPE_BUILTIN_EARPIECE;
        }

        LogUtil.i("Utils", "checkAudioRoute: audioType = " + audioType);

        for (AudioDeviceInfo device : audioDevices) {
            if (device.getType() == audioType) {
                audioDeviceInfo = device;
                break;
            }
        }

        if (audioDeviceInfo == null) {
            LogUtil.i("Utils", "checkAudioRoute: audioDeviceInfo is null");
            return;
        }

        if (audioDeviceInfo != null) {
            boolean result = audioManager.setCommunicationDevice(audioDeviceInfo);
            LogUtil.i("Utils", "checkAudioRoute: setCommunicationDevice result = " + result);
            if (result != true) {
                audioManager.setRouting(AudioManager.MODE_NORMAL, audioDeviceInfo.getId(), AudioManager.ROUTE_ALL);
            }
        }
    }

    static public void switchMic(ISmallControllerService service, int mic) {

    }
}
